﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Library.Helpers
{
    public static class EnumHelper<T>
    {
        public static List<T> ToList(DataTable table, string columnName)
        {
            T enumName;
            List<T> list = new List<T>();
            foreach (DataRow row in table.Rows)
            {
                enumName = ToEnum(NullConverter.FromDbToInt32(row[columnName]));
                list.Add(enumName);
            }

            return list;
        }

        public static T ToEnum(string value)
        {
            return (T)Enum.Parse(typeof(T), value, true);
        }

        public static T ToEnum(int value)
        {
            return (T)Enum.ToObject(typeof(T), value);
        }

        public static T ToEnumFromInteger(object intValue)
        {
            int value = Convert.ToInt32(intValue);
            return ToEnum(value);
        }

        public static string ToIdAsString(T enumValue)
        {
            int value = Convert.ToInt32(enumValue);
            return value.ToString();
        }

        public static int ToIdAsInt(T enumValue)
        {
            return Convert.ToInt32(enumValue);            
        }
    }
}
